package net.qhzw.geologr5.gis.service.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.geologr5.gis.common.framework.utils.JsonResult;
import net.qhzw.geologr5.gis.common.framework.utils.StringUtils;
import net.qhzw.geologr5.gis.service.system.component.FileTemplate;
import net.qhzw.geologr5.gis.service.system.entity.Project;
import net.qhzw.geologr5.gis.service.system.entity.ProjectData;
import net.qhzw.geologr5.gis.service.system.mapper.ProjectDataMapper;
import net.qhzw.geologr5.gis.service.system.mapper.ProjectMapper;
import net.qhzw.geologr5.gis.service.system.mapper.ProjectTypeMapper;
import net.qhzw.geologr5.gis.service.system.query.ProjectDataQuery;
import net.qhzw.geologr5.gis.service.system.query.ProjectItemQuery;
import net.qhzw.geologr5.gis.service.system.service.ProjectItemService;
import net.qhzw.geologr5.gis.service.system.vo.project.ProjectRequest;
import net.qhzw.geologr5.gis.service.system.vo.project.ProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
@Slf4j
public class ProjectItemServiceImpl implements ProjectItemService {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private ProjectDataMapper projectDataMapper;

    @Autowired
    private ProjectTypeMapper projectTypeMapper;

    @Autowired
    private FileTemplate fileTemplate;

    /***
     * 创建项目
     * @param projectRequest
     * @return
     */
    @Override
    public JsonResult create(ProjectRequest projectRequest) {
        log.info("创建项目==ProjectItemServiceImpl==method:create==param: {}", JSON.toJSONString(projectRequest));
        try {
            //修改
            if (projectRequest.getId() != null && projectRequest.getId() > 0) {
                Project project = new Project();
                BeanUtils.copyProperties(projectRequest, project);
                Integer[] projectTypeId = projectRequest.getProjectTypeId();
                if (projectTypeId != null && projectTypeId.length > 0) {
                    project.setProjectTypeId(projectTypeId[projectTypeId.length - 1]);
                }
                int count = projectMapper.updateById(project);
                if (count > 0) {
                    return JsonResult.success();
                }
                return JsonResult.error("修改失败");
            }
            //新增操作
            Project project = new Project();
            BeanUtils.copyProperties(projectRequest, project);
            if (projectRequest.getStartTime() == null) {
                project.setStartTime(new Date());
            }
            Integer[] projectTypeId = projectRequest.getProjectTypeId();
            if (projectTypeId != null && projectTypeId.length > 0) {
                project.setProjectTypeId(projectTypeId[projectTypeId.length - 1]);
            }
            int count = projectMapper.insert(project);
            if (count > 0) {
                return JsonResult.success();
            }
            return JsonResult.error();
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }


    /****
     * 查询项目
     * @param query
     * @return
     */
    @Override
    public JsonResult find(ProjectItemQuery query) {
        Map<String, Object> result = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        try {
            List<Project> projectList = projectMapper.selectProject(query);
            PageInfo<Project> pageInfo = new PageInfo<>(projectList);
            result.put("list", pageInfo.getList());
            result.put("total", pageInfo.getTotal());
            return JsonResult.success(result);
        } catch (DataAccessException e) {
            throw new RuntimeException(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }


    /***
     * 删除项目
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult delete(Integer[] ids) {
        log.info("删除项目==ProjectItemServiceImpl==method:delete==param:{}", ids);
        try {
            List<Integer> idList = Arrays.asList(ids);
            int count = projectMapper.deleteBatchIds(idList);
            if (count > 0) {
                return JsonResult.success();
            }
            return JsonResult.error();
        } catch (Exception e) {
            log.info("删除项目失败==ProjectItemServiceImpl==method:delete==errMsg:{}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }
    }


    /***
     * 项目资料查看
     * @param query
     * @return
     */
    @Override
    public JsonResult selectProjectData(ProjectDataQuery query) {
        Map<String, Object> result = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        try {
            QueryWrapper<ProjectData> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("projectId", query.getProjectId());
            List<ProjectData> projectDataList = projectDataMapper.selectList(null);
            PageInfo<ProjectData> pageInfo = new PageInfo<>(projectDataList);
            result.put("total", pageInfo.getTotal());
            result.put("list", pageInfo.getList());
            return JsonResult.success(result);
        } catch (Exception e) {
            log.info("项目资料查询失败==ProjectItemServiceImpl==method:delete==selectProjectData:{}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }

    }


    /***
     * 查询项目
     * @param query
     * @return
     */
    @Override
    public JsonResult findProject(ProjectItemQuery query) {
        try {
            List<ProjectVo> projectItem = projectMapper.selectProjectItem(query);
            return JsonResult.success(projectItem);
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }

    }



}
